/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.game.disaster.plugin;

import java.awt.Image;
import java.util.ArrayList;
import java.util.Random;
import model.creature.Crystal;
import model.creature.DamageableCreature;
import model.creature.monster.Monster;
import model.creature.plant.Plant;
import model.creature.sprite.Sprite;
import model.game.GameInterface;
import model.game.disaster.Disaster;
import model.utility.ImageUtility;

/**
 * Theft, lost maximum 100 money at the end of day. 10% chance
 */
public class Earthquake extends Disaster {

  /**
   * Keeps track whether this disaster is active
   */
  private boolean active_;

  /**
   * Random number generator
   */
  private Random random_;

  /**
   * Turn happened
   */
  private int turn_;

  /**
   * Constructor
   */
  public Earthquake(GameInterface game) {
    super(game);
    active_ = false;
    random_ = new Random();
  }

  @Override
  public String getName() {
    return "Earthquake";
  }

  @Override
  public String getDescription() {
    return "Damages all creatures by 20%";
  }

  @Override
  public void advanceDay() {
    active_ = false;
    if (game_.getGameTime().getDayCount() >= 0) {

      // 10% chance of happening every day
      if (random_.nextDouble() > 0.90) {

        game_.addImportantMessage("The weather forecast predicts an earthquake today at night.");
        active_ = true;
      }
    }
  }

  @Override
  public void advanceTime() {
    if (game_.isNight()) {
      if (active_) {
        if (game_.getNightManager().getElapsedTime() == 0) {
          turn_ = random_.nextInt(game_.getNightManager().getCurrentNightLength());
        }
      }
    }
    if (game_.isNight() && active_ && game_.getNightManager().getElapsedTime() == turn_) {
      // happens
      double damage = random_.nextDouble() * 0.3;
      game_.addImportantMessage("An earthquake has just happened with power of " + damage);

      ArrayList<DamageableCreature> crts =
              (ArrayList<DamageableCreature>)game_.getCreatures().getAllDamageableCreatures();
      for (DamageableCreature crt : crts) {
        crt.inflictDamage((int)Math.round(Math.floor((double)crt.getHP() * damage)));
      }

    }
  }

  @Override
  public boolean isActive() {
    return active_;
  }

  public Image getImage() {
    return ImageUtility.loadImage("resources/pictures/disaster/earthquake.png");
  }

}
